//
//  AppDelegate.m
//  weibo-OC
//
//  Created by 黄力 on 15/12/15.
//  Copyright © 2015年 HLTest. All rights reserved.
//

#import "AppDelegate.h"
#import "HLMainController.h"
#import "HLGudieController.h"
#import "HLWelcomeController.h"
#import "HLUserAccountViewModel.h"
#import "HLComposeController.h"
#import "HLComposePictureController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //注册成为监听者
    [self regisetrObserver];
    //创建window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //设置根控制器
    self.window.rootViewController = [self showRootController];
//    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[HLComposeController alloc] init]];
    //设置当前窗口为可见主窗口
    [self.window makeKeyAndVisible];
    return YES;
}

//获取登录时要显示的根控制器
- (UIViewController *)showRootController {
    //判断是否登录
    if ([HLUserAccountViewModel getUserLogin]) {
        //判断是否新版本
        if ([self compareVisionNum]) {
            return [HLGudieController gudieControllerWithFlewLayout];
        }
        return [[HLWelcomeController alloc] init];
    }
    return [[HLMainController alloc] init];
}

//判断是否是新版本
- (BOOL)compareVisionNum {
    //获取当前版本
    CGFloat newVisionNum = [[NSBundle mainBundle].infoDictionary[@"CFBundleVersion"] floatValue];
    //获取偏好设置对象
    NSUserDefaults *df = [NSUserDefaults standardUserDefaults];
    //获取保存的版本号
    CGFloat saveVisionNum = [df doubleForKey:@"visionNum"];
    //比较
    if (newVisionNum > saveVisionNum) {
        //保存新版本号
        [df setDouble:newVisionNum forKey:@"visionNum"];
        return YES;
    }
    return NO;
}

//注册成为监听者
- (void)regisetrObserver {
    //注册监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeWindowRootController:) name:MainWindowChangeRootControllerNotification object:nil];
}

//接收到通知时调用
- (void)changeWindowRootController:(NSNotification *)notification {
    //根据传递过来的是否有参数来判断要修改的根控制器类型
    if (notification.object) {
        self.window.rootViewController = [[HLMainController alloc] init];
        return;
    }
    self.window.rootViewController = [[HLWelcomeController alloc] init];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
